package cn.silver.framework.openapi.model;

import cn.silver.framework.openapi.constant.ResultEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * 表格分页数据对象
 *
 * @author Silver Zhou
 */

@Data
@NoArgsConstructor
@Schema(name = "ResultPage", description = "分页查询返回对象")
public class ResultPage<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 消息状态码
     */
    @Schema(title = "响应码")
    private int code;

    /**
     * 消息内容
     */
    @Schema(title = "响应消息")
    private String msg;


    /**
     * 总记录数
     */
    @Schema(title = "记录总数")
    private long total;

    /**
     * 列表数据
     */
    @Schema(title = "列表数据")
    private List<T> rows;

    /**
     * 分页
     *
     * @param list  列表数据
     * @param total 总记录数
     */
    public ResultPage(List<T> list, long total) {
        this.rows = list;
        this.total = total;
    }

    public static <T> ResultPage<T> build() {
        ResultPage<T> rspData = new ResultPage<>();
        rspData.setCode(ResultEnum.SUCCESS.getCode());
        rspData.setMsg("查询成功");
        return rspData;
    }

    public static <T> ResultPage<T> build(List<T> list) {
        ResultPage<T> result = new ResultPage<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("查询成功");
        result.setRows(list);
        result.setTotal(list.size());
        return result;
    }

    public static <T> ResultPage<T> build(List<T> list, long total) {
        ResultPage<T> result = new ResultPage<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("查询成功");
        result.setRows(list);
        result.setTotal(total);
        return result;
    }
}
